using System.Collections.Generic;
using model.entity;
using UnityEngine;

public class GunManager : SingletonAutoMono<GunManager>
{
    
    private Dictionary<string, GunData> guns = new Dictionary<string, GunData>();
    private Dictionary<string, BulletData> bullets = new Dictionary<string, BulletData>();

    void Start()
    {
        LoadAllData();
    }
    
    private void LoadAllData()
    {
        // 加载枪械数据
        // TextAsset gunsJson = Resources.Load<TextAsset>("./Res/Data/guns");
        TextAsset gunsJson = ABMgr.GetInstance().LoadAssetBundleSync<TextAsset>("data", "guns");
        GunList gunList = JsonUtility.FromJson<GunList>(gunsJson.text);
        
        foreach (GunData gun in gunList.guns)
        {
            gun.LoadResources();
            guns.Add(gun.id, gun);
        }
        
        // 加载子弹数据
        // TextAsset bulletsJson = Resources.Load<TextAsset>("./Res/Data/bullets");
        TextAsset bulletsJson = ABMgr.GetInstance().LoadAssetBundleSync<TextAsset>("data", "bullets");
        BulletList bulletList = JsonUtility.FromJson<BulletList>(bulletsJson.text);
        
        foreach (BulletData bullet in bulletList.bullets)
        {
            bullet.LoadResources();
            bullets.Add(bullet.id, bullet);
        }
    }
    
    public GunData GetGun(string id)
    {
        if (guns.ContainsKey(id))
            return guns[id];
        return null;
    }
    
    public BulletData GetBullet(string id)
    {
        if (bullets.ContainsKey(id))
            return bullets[id];
        return null;
    }

    public Dictionary<string, GunData> GetGuns()
    {
        return guns;
    }
    
    public Dictionary<string, BulletData> Getbullets()
    {
        return bullets;
    }
    
    // 辅助类用于JSON解析
    [System.Serializable]
    private class GunList { public GunData[] guns; }
    
    [System.Serializable]
    private class BulletList { public BulletData[] bullets; }
}